<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>知识图谱补全系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 左侧面板 -->
            <div class="col-md-4 sidebar">
                <div class="d-flex justify-content-between align-items-center my-4">
                    <h2>知识图谱补全系统</h2>
                    <div class="dropdown">
                        <button class="btn btn-outline-secondary dropdown-toggle" type="button" id="userMenu" data-bs-toggle="dropdown" aria-expanded="false">
                            欢迎，{{ name }}
                        </button>
                        <ul class="dropdown-menu" aria-labelledby="userMenu">
                            <li><a class="dropdown-item" href="{{ url_for('logout') }}">退出登录</a></li>
                        </ul>
                    </div>
                </div>
                
                {% with messages = get_flashed_messages(with_categories=true) %}
                    {% if messages %}
                        {% for category, message in messages %}
                            <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                                {{ message }}
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                            </div>
                        {% endfor %}
                    {% endif %}
                {% endwith %}
                
                <!-- 设置知识图谱 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5>设置实体和关系</h5>
                    </div>
                    <div class="card-body">
                        <form action="/setup" method="post">
                            <div class="mb-3">
                                <label for="entities" class="form-label">已知实体列表（用逗号分隔）</label>
                                <textarea class="form-control" id="entities" name="entities" rows="3" placeholder="例如: 北京,中国,上海,长江">{{ ','.join(known_entities) }}</textarea>
                            </div>
                            <div class="mb-3">
                                <label for="relations" class="form-label">已知关系列表（用逗号分隔）</label>
                                <textarea class="form-control" id="relations" name="relations" rows="2" placeholder="例如: 位于,流经,首都是">{{ ','.join(known_relations) }}</textarea>
                            </div>
                            <button type="submit" class="btn btn-primary">设置</button>
                        </form>
                    </div>
                </div>
                
                <!-- 三元组提取 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5>从文本提取三元组</h5>
                    </div>
                    <div class="card-body">
                        <ul class="nav nav-tabs mb-3" id="extractionTabs" role="tablist">
                            <li class="nav-item" role="presentation">
                                <button class="nav-link active" id="text-tab" data-bs-toggle="tab" data-bs-target="#text-content" type="button" role="tab" aria-controls="text-content" aria-selected="true">文本输入</button>
                            </li>
                            <li class="nav-item" role="presentation">
                                <button class="nav-link" id="file-tab" data-bs-toggle="tab" data-bs-target="#file-content" type="button" role="tab" aria-controls="file-content" aria-selected="false">文件上传</button>
                            </li>
                        </ul>
                        <div class="tab-content" id="extractionTabContent">
                            <!-- 文本输入标签页 -->
                            <div class="tab-pane fade show active" id="text-content" role="tabpanel" aria-labelledby="text-tab">
                                <form action="/extract" method="post">
                                    <div class="mb-3">
                                        <label for="text" class="form-label">输入文本</label>
                                        <textarea class="form-control" id="text" name="text" rows="4" placeholder="例如: 北京是中国的首都。上海位于长江入海口。"></textarea>
                                    </div>
                                    <button type="submit" class="btn btn-success">提取三元组</button>
                                </form>
                            </div>
                            <!-- 文件上传标签页 -->
                            <div class="tab-pane fade" id="file-content" role="tabpanel" aria-labelledby="file-tab">
                                <form action="/extract_file" method="post" enctype="multipart/form-data">
                                    <div class="mb-3">
                                        <label for="file" class="form-label">上传文本文件</label>
                                        <input type="file" class="form-control" id="file" name="file" accept=".txt,.doc,.docx,.pdf">
                                        <div class="form-text">支持的文件格式: TXT、DOC、DOCX、PDF</div>
                                    </div>
                                    <button type="submit" class="btn btn-success">提取三元组</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 显示新发现的三元组 -->
                <div class="card mb-4">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5>新发现的三元组</h5>
                        <div class="btn-group" role="group">
                            <form action="/load_demo" method="post" style="margin: 0;">
                                <button type="submit" class="btn btn-sm btn-outline-primary me-2">加载演示数据</button>
                            </form>
                            <form action="/clear" method="post" style="margin: 0;">
                                <button type="submit" class="btn btn-sm btn-outline-danger">清空图谱</button>
                            </form>
                        </div>
                    </div>
                    <div class="card-body">
                        <ul class="list-group" id="newTriples">
                            {% for head, relation, tail in new_triples %}
                            <li class="list-group-item text-danger">{{ head }} - {{ relation }} - {{ tail }}</li>
                            {% endfor %}
                        </ul>
                        {% if not new_triples %}
                        <p class="text-muted">尚未发现新的三元组</p>
                        {% endif %}
                    </div>
                </div>
            </div>
            
            <!-- 右侧知识图谱可视化 -->
            <div class="col-md-8 graph-container">
                <iframe id="graphFrame" src="{{ url_for('static', filename='js/graph.html') }}" frameborder="0"></iframe>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
</body>
</html>
